<?php
namespace app\index\service;
use think\Db;
class  Order{

    //律师函与劳动合同 订单详情
    function orderData($type=1){
        if($type == 1){
            $service_name = '律师函';
            $pay_price    = 580;
        }else{
            $service_name = '企业劳动风险防范专项套餐';
            $pay_price    = 888;
        }
        $data['service_name'] = $service_name;
        $data['pay_price']    = $pay_price;
        $data['type_id']      = $type;
        return $data;
    }

    //查询订单
    function orderPay($o_no){
        $data = Db::name('order')->where('o_no',$o_no)->field('o_no,o_type_id,o_money,o_comment,o_addtime,o_state')->find();
        if($data['o_state'] != 1) $data = null;
        return $data;
    }

    //订单生成
    function orderCreateApi($type_id,$content){
       try{
        if(empty($type_id)){
            return messageInfo('101', '系统参数错误!');
        }
        $lt_user = session('lt_user');
        if($type_id == 1){
            $pay_price    = 580;
        }else{
            $pay_price    = 888;
        }
        $data_array = array(
            'u_uid'     => $lt_user['u_uid'],
            'o_type_id' => $type_id,
            'o_money'     => $pay_price,
            'o_addtime'     => date('Y-m-d H:i:s')
        );
        if(!empty($content)) $data_array['o_comment'] = $content;
        $order = Db::name('order');
        $order->startTrans();
        $a     = $order->insert($data_array);
        $order_id = $order->getLastInsID();
        if(!$a){
            $order->rollback();
            return messageInfo('101', '提交失败!');
        }
        $o_no = date('YmdHis').'_'.$order_id;
        $b      = $order->where('order_id',$order_id)->setField('o_no',$o_no);
        if(!$b){
            $order->rollback();
            return messageInfo('101', '提交失败!');
        }
        $order->commit();
        $url = config('tempredirect_url').'service/order_pay/o_no/'.$o_no;
        return messageInfo('202', '提交成功!','',$url);
       } catch (\Exception $e) {
           $response = messageInfo ( '101', '服务器错误！' );
       }
        return $response;
    }

    //查询订单是否支付成功
    function orderStateApi($o_no){
        if(empty($o_no)){
            return messageInfo('101', '系统参数错误!');
        }
        $o_state = Db::name('order')->where('o_no',$o_no)->value('o_state');
        if($o_state == 1){
            return messageInfo('101', '未支付!');
        }else{
            $url = config('tempredirect_url').'user/myorder';
            return messageInfo('202', '支付成功!','',$url);
        }
    }

    //服务卡，订单信息
    function serveOrder($sid){
        $data = Db::name('counselor')->where('counselor_id',$sid)->field('ck_name,ck_price,counselor_id')->find();
        return $data;
    }

    //顾问卡，订单
    function serveOrderPresentApi($type_id){
        try{
            if(empty($type_id))  return messageInfo('101', '系统参数错误!');
            $type_id     = intval($type_id);
            if($type_id != 11 && $type_id != 12 && $type_id != 13 && $type_id != 14 &&  $type_id != 2 && $type_id != 3 && $type_id != 4 && $type_id != 5 && $type_id != 6 && $type_id != 7){
                return messageInfo('101', '购买项目不存在!');
            }
            $product = product($type_id);
            if($type_id == 11){
                $od_sum      = 2;
                $pay_price   = $product['price'] * $od_sum;//顾问基础套餐。二年起
            }else{
                $pay_price   = $product['price'];
                $od_sum      = 1;
            }
            $unit_price    = $product['price'];

            if($pay_price <= 0 ){
                if(empty($data)) return messageInfo('101', '项目不存在!');
            }
            $lt_user = session('lt_user');
            $data_array = array(
                'u_uid'          => $lt_user['u_uid'],
                'o_type_id'      => $type_id,
                'o_money'        => $pay_price,
                'o_addtime'      => date('Y-m-d H:i:s')
            );
            if(!empty($content)) $data_array['o_comment'] = $content;
            $order = Db::name('order');
            $order->startTrans();
            $a     = $order->insert($data_array);
            $order_id = $order->getLastInsID();

            //写入详情表
            $data_array_details = array(
              'order_id'  => $order_id,
              'od_name'   => product_name($type_id),
              'od_price'  => $unit_price,
                'od_sum'   => $od_sum
            );

            $bc = Db::name('order_details')->insert($data_array_details);
            if(!$a || !$bc){
                $order->rollback();
                return messageInfo('101', '购买失败!');
            }
            $o_no = date('YmdHis').'_'.$order_id;
            $b      = $order->where('order_id',$order_id)->setField('o_no',$o_no);
            if(!$b){
                $order->rollback();
                return messageInfo('101', '购买失败!');
            }
            $order->commit();
            $url = config('tempredirect_url').'service/order_pay/o_no/'.$o_no;
            return messageInfo('202', '购买成功!','',$url);
        } catch (\Exception $e) {
            $response = messageInfo ( '101', '服务器错误！' );
        }
        return $response;
    }

    //顾问卡详情
    function servedDetails($sno){
         $data = Db::name('counselor_order')->where('co_no',$sno)->field('co_name,co_no,co_money,co_age_limit,co_content,co_addtime,co_ps,co_begintime,co_finishtime')->find();
         $data['list'] = explode('&',$data['co_ps']);
         unset($data['co_ps']);
         return $data;
    }

    //订单取消
    function userOrderCancelApi($o_no,$type){
        if(empty($o_no) || empty($type)){
            return messageInfo('101', '系统参数错误!');
        }
        $order   = Db::name('order');
        $lt_user = session('lt_user');
        $where['o_no'] = $o_no;
        $where['u_uid'] = $lt_user['u_uid'];
        $o_state = $order->where($where)->value('o_state');
        if($o_state != 1){
            return messageInfo('101', '订单状态不允许操作!');
        }
        if($type == 1 || $type == 3){
            $return_name = '订单取消';
            $value =  4;
            if($type == 1){
                $url = config('tempredirect_url').'user/not_myorder';
            }else{
                $url = config('tempredirect_url').'user/myorder';
            }
        }else{
            $return_name = '转账确认申请';
            $value =  5;
            $url = config('tempredirect_url').'service/order_pay/o_no/'.$o_no;
        }
        $data_array['o_handletime'] = date('Y-m-d H:i:s');
        $data_array['o_state']      = $value;
        $a =  $order->where($where)->update($data_array);
        if(!$a){
            return messageInfo('101', $return_name.'失败!');
        }
        return messageInfo('202', $return_name.'成功!','',$url);
    }

}
